package com.oceansoft.mobile.econsole.modules.log.controller;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.oceansoft.mobile.econsole.modules.apps.entity.AppDownload;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.log.service.ILoginLogService;
import com.oceansoft.mobile.econsole.modules.member.entity.Member;

@Controller
@RequestMapping("sys/login")
public class LoginLogController extends BaseController {
	@Resource
	ILoginLogService loginLogService;
	@RequestMapping(value="index")
	public String gotoLoginLogIndex(Query query,Model model){

		        
		        model.addAttribute("query", query);
		        model.addAttribute("pager", loginLogService.getLoginLogPager(query));
		     
		        return "log/loginlog-list";
		
	}
	
	
	 /**
     * 列表分页显示
     *
     * @param query 分页查询条件
     * @param model 数据Model
     * @return 页面地址
     */
    @RequestMapping(value = "list", method = POST)
    public String loadPageByCondition(Query query, Model model) {
        Pager pager = loginLogService.findLoginMainByCondition(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
     
        return "log/loginlog-list";
    }
    
    
    /**
     * 统计量导出
     *
     * @return
     */
    @RequestMapping("exportExcel.xls")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Query query) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      //  String newdate = sdf.format(new Date());
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("登陆量统计");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short)0);  
        cell.setCellValue("登陆账号");  
        cell.setCellStyle(style);  
        cell = row.createCell((short)1);  
        cell.setCellValue("登陆时间");  
        cell.setCellStyle(style);  
     
      
    
  
        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
        List<Member> list= loginLogService.loadExcel(query); 
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);  
           Member mb = list.get(i);  
            // 第四步，创建单元格，并设置值  
           row.createCell((short)0).setCellValue(mb.getLoginId()); 
           row.createCell((short)1).setCellValue(sdf.format(mb.getLoginTime())); 
         
        
        }  
        // 第六步，将文件存到指定位置  
        try  
        {
        	response.setContentType("application/ms-excel");
        	OutputStream out = response.getOutputStream();
        	wb.write(out);  
            out.close();  
        }
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }
    




}
